/*
 * Copyright (C) 2016 Christian Schmitz
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package berlin.volders.badger.example.slice;

import berlin.volders.badger.BadgeShape;
import berlin.volders.badger.CountBadge;
import berlin.volders.badger.example.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.components.Text;
import ohos.agp.utils.Color;
import ohos.agp.utils.TextAlignment;

public class BadgerAbility extends AbilitySlice implements Component.ClickedListener {
    public static final float NUM_D5F = .5f;
    public static final float NUM_2F = 2f;

    CountBadge.Factory ovalFactory;
    CountBadge.Factory squareFactory;
    CountBadge.Factory circleFactory;
    private Text textOval, textSquare, textCircle;
    private Button reset;
    private int ovalCount = 0;
    private int squareCount = 0;
    private int circleCount = 0;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_ability_badger);
        getWindow().setStatusBarColor(Color.getIntColor("#1C76D2"));
        ovalFactory = new CountBadge.Factory(this, BadgeShape.oval(1f, NUM_2F, TextAlignment.BOTTOM));
        squareFactory = new CountBadge.Factory(this, BadgeShape.square(1f, TextAlignment.VERTICAL_CENTER, NUM_D5F));
        circleFactory = new CountBadge.Factory(this, new CustomBadgeShape(
                this, NUM_D5F, TextAlignment.END | TextAlignment.TOP));

        initComponent();
    }

    private void initComponent() {
        Image imgCircle = (Image) findComponentById(ResourceTable.Id_img_circle);
        Image imgSquare = (Image) findComponentById(ResourceTable.Id_img_square);
        Image imgOval = (Image) findComponentById(ResourceTable.Id_img_oval);
        reset = (Button) findComponentById(ResourceTable.Id_reset);

        imgCircle.setClickedListener(this);
        imgSquare.setClickedListener(this);
        imgOval.setClickedListener(this);
        reset.setClickedListener(this);
        textOval = (Text) findComponentById(ResourceTable.Id_text_oval);
        textSquare = (Text) findComponentById(ResourceTable.Id_text_square);
        textCircle = (Text) findComponentById(ResourceTable.Id_text_circle);
    }

    @Override
    public void onClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_img_circle:
                textCircle.setVisibility(Component.VISIBLE);
                circleCount += 1;
                textCircle.setText(circleCount + "");
                break;
            case ResourceTable.Id_img_square:
                textSquare.setVisibility(Component.VISIBLE);
                squareCount += 1;
                textSquare.setText(squareCount + "");
                break;
            case ResourceTable.Id_img_oval:
                textOval.setVisibility(Component.VISIBLE);
                ovalCount += 1;
                textOval.setText(ovalCount + "");
                break;
            case ResourceTable.Id_reset:
                textOval.setVisibility(Component.HIDE);
                textSquare.setVisibility(Component.HIDE);
                textCircle.setVisibility(Component.HIDE);
                ovalCount = 0;
                squareCount = 0;
                circleCount = 0;
                break;
            default:
                break;
        }
    }
}
